/*
 * Created by JFormDesigner on Tue Apr 17 15:44:09 EEST 2012
 */

package tr.gov.tubitak.bilgem.esya.crlviewer;


import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author Haydar Karabas
 */
public class ECrlRevokedCertsPanel extends JPanel {
    private EViewerCRL viewerCRL;
    ERevokedCertsModel revokedCertsModel;
    RowFilter<Object,Object> serialNumberFilter;
    TableRowSorter<ERevokedCertsModel> rowSorter;

    public ECrlRevokedCertsPanel(EViewerCRL viewerCRL) {
        initComponents();
        this.viewerCRL = viewerCRL;
        loadCRL();
        initGUI();
    }

    private void initGUI() {
        rowSorter = new TableRowSorter<ERevokedCertsModel>(revokedCertsModel);
        rowSorter.setComparator(1,new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                EDate dt1= (EDate) o1;
                EDate dt2 = (EDate) o2;
                return dt1.compareTo(dt2);
            }
        });
        rowSorter.setRowFilter(serialNumberFilter);
        eRevokedCertsTable1.setRowSorter(rowSorter);
        List<RowSorter.SortKey> sortKeys
                = new ArrayList<RowSorter.SortKey>();
        sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
        rowSorter.setSortKeys(sortKeys);
    }

    void loadCRL(){
        revokedCertsModel = new ERevokedCertsModel();
        eRevokedCertsTable1.setModel(revokedCertsModel);
        revokedCertsModel.loadRevocationList(viewerCRL.getRevokedCerts());
    }

    private void btnFilterActionPerformed(ActionEvent e) {
        serialNumberFilter = new RowFilter<Object,Object>() {
            public boolean include(Entry<? extends Object, ? extends Object> entry) {
                for (int i = entry.getValueCount() - 1; i >= 0; i--) {
                    String stringValue = entry.getStringValue(i);
                    stringValue = stringValue.trim();
                    if (stringValue.startsWith(txtSerialNumberFilter.getText())) {
                        return true;
                    }
                }
                return false;
            }
        };
        rowSorter.setRowFilter(serialNumberFilter);
        revokedCertsModel.fireTableDataChanged();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        ResourceBundle bundle = ResourceBundle.getBundle("tr.gov.tubitak.bilgem.esya.crlviewer.lang");
        panel1 = new JPanel();
        txtSerialNumberFilter = new JTextField();
        btnFilter = new JButton();
        lblRevokedCerts = new JLabel();
        scrollPane1 = new JScrollPane();
        eRevokedCertsTable1 = new ERevokedCertsTable();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {10, 0, 10, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {10, 0, 0, 0, 10, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 1.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0, 0.0, 1.0E-4};

        //======== panel1 ========
        {
            panel1.setBorder(new TitledBorder(bundle.getString("ECrlRevokedCertsPanel.panel1.border")));
            panel1.setLayout(new GridBagLayout());
            ((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {289, 20, 0};
            ((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {0, 0};
            ((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0E-4};
            ((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

            //---- txtSerialNumberFilter ----
            txtSerialNumberFilter.setToolTipText("A\u015fa\u011f\u0131daki tablo alanlar\u0131 i\u00e7erisinde filtreleme yapmak i\u00e7in. Seri numaras\u0131 i\u00e7in 00 0f \u015feklinde giriniz.");
            panel1.add(txtSerialNumberFilter, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 5), 0, 0));

            //---- btnFilter ----
            btnFilter.setText(bundle.getString("ECrlRevokedCertsPanel.btnFilter.text"));
            btnFilter.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    btnFilterActionPerformed(e);
                }
            });
            panel1.add(btnFilter, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        }
        add(panel1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

        //---- lblRevokedCerts ----
        lblRevokedCerts.setText(bundle.getString("ECrlRevokedCertsPanel.lblRevokedCerts.text"));
        lblRevokedCerts.setFont(lblRevokedCerts.getFont().deriveFont(lblRevokedCerts.getFont().getStyle() | Font.BOLD, lblRevokedCerts.getFont().getSize() + 1f));
        add(lblRevokedCerts, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(eRevokedCertsTable1);
        }
        add(scrollPane1, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JTextField txtSerialNumberFilter;
    private JButton btnFilter;
    private JLabel lblRevokedCerts;
    private JScrollPane scrollPane1;
    private ERevokedCertsTable eRevokedCertsTable1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
